<?php
/**
 * Created by PhpStorm.
 * User: aha
 * Date: 2016/9/13
 * Time: 21:05
 */

namespace Exam\Test;



class Test
{
     public $Document;
     public $namespace;

     /**
      * @param $type  想要生成的数据类型。0 ：bool 1:int 2 :char 3:中文  default:包含前面4种类型的字符串。
      * @param $length 生成的数据长度。默认长度为2；
      */
     function myRandom($type=5,$length=2){
          $chars = '0123456789ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz,./!@#$%^&*()_+';
          switch ($type){
               case 0://bool
                   return rand(0,1)?true:false;
               case 1://int
                   return rand(-2147483648,2147483647);
//                   return rand(0,4294967296);
              case 2://char
                  $str = '';
                  $len = strlen($chars);
                  for ($i=0; $i<$length; $i++){
                      $str .= substr($chars,rand(0,$len),1);
                  }
                  return $str;
              case 3://中文
                 $chines = function(){
                     $content = chr(rand(0xB0,0xCC)).chr(rand(0xA1,0xBB));
                     return mb_convert_encoding($content,'UTF-8','GBK');
                 };
                  $str = '';
                  for ($i=0; $i<$length; $i++){
                      $str .= $chines();
                  }
                  return $str;
               default:  $str = '';
                   for ($i=0; $i<$length; $i++){
                       $str .= $this->myRandom(rand(0,5),1);
                   }
                   return $str;
          }
     }

     function __construct($pdm,$namespace='/Management/Object/')
     {
          $this->Document = new \Document();
          $this->namespace = $namespace;
          $this->Document->loadPDM($pdm);
     }

     function testAll($Object){
         $this->testSave($Object);
         $this->testAdd($Object);
         $this->testGet($Object);
         $this->testDel($Object);
     }

    /**
     * 生成随机的外键ID。
     * @param ID $
     * @param Fks $
     */
    function getFKId($IDname,$Fks){
//        echo 'test';
        foreach ($Fks as $fk){
            foreach ($fk as $value){
                if ($value['ChildTableColumnCode']==$IDname){
//                    $str = $value['ParentTableCode'];
                     strtok($value['ParentTableCode'],"}");
                    $str = strtok("}");
                    $a = M($str)->limit(1)->field($value['ParentTableColumnCode'])->select();
                   return $a[0][$value['ParentTableColumnCode']];
                }
            }
        }
    }

    //在save方法
     function testSave($Object){
          $PDMdata = \Document::$docs['PDM']['Tables'][parse_name($Object)];
          $Class = str_replace('Test','Object',__NAMESPACE__).'\\'.$Object.'Object';
          $Object = new $Class;
         $oldData = M($Object->main)->select();
         $oldData = $oldData[rand(0,count($oldData))];
         $ID =$oldData[$Object->pk];
         foreach ($PDMdata['Columns'] as $key=> $value){
             //包含ID分为：主键、外键
             $data[$key] = $oldData[$key];
             if (strstr($key,'ID')){
                 //在修改的时候不允许修改主键。
                 if ($value['P'])   continue;
//                 $t2 = lcfirst($Object->main);
//                 $t3 = \Document::$docs['PDM']['Tables'][lcfirst($Object->main)]['FKs'];
                 $temp = $this->getFKId($key, \Document::$docs['PDM']['Tables'][lcfirst($Object->main)]['FKs']);
                 if ($temp){
                     $data[$key] = $temp;
                 }
                 continue;
             }

             $value['DataType'] = explode("(",$value['DataType']);
            $value['DataType'][1] = trim($value['DataType'][1],")");
               switch ($value['DataType'][0]){
                    case 'int':
                        $data[$key] = $this->myRandom(1);
                        break;
                   case 'char':
                       $data[$key] = $this->myRandom(5,rand(1,$value['DataType'][1]/4));
                       break;
                   default: $data[$key] = $this->myRandom(1,rand(1,10));
               }
          }
         $newData = $Object->save($ID,$data);

         if ($newData){
             echo "修改成功".PHP_EOL;
         }else{
             echo "修改失败".PHP_EOL;
         }

//         print_r($oldData);
//         echo PHP_EOL;
//         print_r($newData);
//         echo strlen($data['Title'])."".PHP_EOL;
//         echo strlen($data['Address'])."".PHP_EOL;
          return;
     }
     function testAdd($Object){
         $PDMdata = \Document::$docs['PDM']['Tables'][parse_name($Object)];
         $Class = str_replace('Test','Object',__NAMESPACE__).'\\'.$Object.'Object';
         $Object = new $Class;
         foreach ($PDMdata['Columns'] as $key=> $value){
             //包含ID分为：主键、外键
             if (strstr($key,'ID')){
                 //在修改的时候不允许修改主键。
                 if ($value['P'])   continue;
//                 $t2 = lcfirst($Object->main);
//                 $t3 = \Document::$docs['PDM']['Tables'][lcfirst($Object->main)]['FKs'];
                 $temp = $this->getFKId($key, \Document::$docs['PDM']['Tables'][lcfirst($Object->main)]['FKs']);
                 if ($temp){
                     $data[$key] = $temp;
                 }else{
                     $data[$key] = 0;
                 }
                 continue;
             }

             $value['DataType'] = explode("(",$value['DataType']);
             $value['DataType'][1] = trim($value['DataType'][1],")");
             switch ($value['DataType'][0]){
                 case 'int':
                     $data[$key] = $this->myRandom(1);
                     break;
                 case 'char':
                     $data[$key] = $this->myRandom(5,rand(1,$value['DataType'][1]/4));
                     break;
                 default: $data[$key] = $this->myRandom(1,rand(1,10));
             }
         }
//         $newData = $Object->save($ID,$data);
         $newData = $Object->add($data);

         if ($newData){
             echo "添加成功".PHP_EOL;
         }else{
             echo "添加失败".PHP_EOL;
         }

//         print_r($oldData);
//         echo PHP_EOL;
//         print_r($newData);
//         echo strlen($data['Title'])."".PHP_EOL;
//         echo strlen($data['Address'])."".PHP_EOL;
         return;
     }
     function testGet($Object){
         $Class = str_replace('Test','Object',__NAMESPACE__).'\\'.$Object.'Object';
         $Object = new $Class;
         if ($Object->allow_del === false){
             echo "allow_del为false".PHP_EOL;
             return;
         }
         $ID = M($Object->main)->field($Object->pk)->select();
         $ID = $ID[rand(0,count($ID))][$Object->pk];
        print_r($Object->get($ID));
         return;

     }
     function testDel($Object){
//         $PDMdata = \Document::$docs['PDM']['Tables'][parse_name($Object)];
         $Class = str_replace('Test','Object',__NAMESPACE__).'\\'.$Object.'Object';
         $Object = new $Class;
         if ($Object->allow_del === false){
             echo "allow_del为false".PHP_EOL;
             return;
         }
         $ID = M($Object->main)->field($Object->pk)->select();
         $ID = $ID[rand(0,count($ID))][$Object->pk];
         $Object->del($ID);
         echo "$ID".PHP_EOL;
         if ($Object->get($ID)){
             echo "删除失败".PHP_EOL;
         }else{
             echo "删除成功".PHP_EOL;
         }
         return;
     }

    function testSearch($Object){

    }
}